home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
Swar
/
ships.c
< prev
next >
Wrap
Text File
|
1994-08-21
|
6KB
|
214 lines
#include "swar.h"
#define MAX_SPEED 3
InitShips()
{
extern RGBColor myWhite, myBlack, myRed, myBlue, myYellow, myGreen, myOrange, myGray, myDkBlue;
extern SHIPREC gShipRecs[MAX_PLAYERS], gOldShipRecs[MAX_PLAYERS];
extern Rect gShipSrcRects[MAX_PLAYERS][8][8];
short i, j;
for (i = 0; i < MAX_PLAYERS; i++) {
gShipRecs[i].where.v = RngRnd(25, 455);
gShipRecs[i].where.h = RngRnd(25, 615);
if (i) {
gShipRecs[i].vel.v = RngRnd(-MAX_SPEED, MAX_SPEED);
gShipRecs[i].vel.h = RngRnd(-MAX_SPEED, MAX_SPEED);
} /* if */
else {
gShipRecs[i].vel.v = 0;
gShipRecs[i].vel.h = 0;
} /* else */
gShipRecs[i].dir = RngRnd(0, 7);
gShipRecs[i].anim = RngRnd(0, 7);
gShipRecs[i].isAlive = TRUE;
gShipRecs[i].isAccel = FALSE;
} /* for */
gShipRecs[0].color = myBlue;
gShipRecs[1].color = myRed;
gShipRecs[2].color = myYellow;
gShipRecs[3].color = myGreen;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++) {
SetRect(&(gShipSrcRects[0][i][j]), 4 + (20 * j), 12 + (20 * i), 19 + (20 * j), 27 + (20 * i));
SetRect(&(gShipSrcRects[1][i][j]), 164 + (20 * j), 12 + (20 * i), 179 + (20 * j), 27 + (20 * i));
SetRect(&(gShipSrcRects[2][i][j]), 4 + (20 * j), 172 + (20 * i), 19 + (20 * j), 187 + (20 * i));
SetRect(&(gShipSrcRects[3][i][j]), 164 + (20 * j), 172 + (20 * i), 179 + (20 * j), 187 + (20 * i));
} /* for */
} /* InitShips() */
MoveShips()
{
short i;
for (i = 0; i < MAX_PLAYERS; i++)
if (gShipRecs[i].isAlive) {
if (gShipRecs[i].isAccel)
switch (gShipRecs[i].dir) {
case 0: gShipRecs[i].vel.v -= 1; break;
case 1: gShipRecs[i].vel.v -= 1; gShipRecs[i].vel.h += 1; break;
case 2: gShipRecs[i].vel.h += 1; break;
case 3: gShipRecs[i].vel.v += 1; gShipRecs[i].vel.h += 1; break;
case 4: gShipRecs[i].vel.v += 1; break;
case 5: gShipRecs[i].vel.v += 1; gShipRecs[i].vel.h -= 1; break;
case 6: gShipRecs[i].vel.h -= 1; break;
case 7: gShipRecs[i].vel.v -= 1; gShipRecs[i].vel.h -= 1; break;
} /* switch */
if (gShipRecs[i].vel.h > MAX_SPEED)
gShipRecs[i].vel.h = MAX_SPEED;
if (gShipRecs[i].vel.h < -MAX_SPEED)
gShipRecs[i].vel.h = -MAX_SPEED;
if (gShipRecs[i].vel.v > MAX_SPEED)
gShipRecs[i].vel.v = MAX_SPEED;
if (gShipRecs[i].vel.v < -MAX_SPEED)
gShipRecs[i].vel.v = -MAX_SPEED;
gShipRecs[i].where.h += gShipRecs[i].vel.h;
gShipRecs[i].where.v += gShipRecs[i].vel.v;
if (gShipRecs[i].where.h > 624) {
gShipRecs[i].vel.h *= -1;
gShipRecs[i].where.h = 624;
} /* if */
if (gShipRecs[i].where.h < 0) {
gShipRecs[i].vel.h *= -1;
gShipRecs[i].where.h = 0;
} /* if */
if (gShipRecs[i].where.v > 464) {
gShipRecs[i].vel.v *= -1;
gShipRecs[i].where.v = 464;
} /* if */
if (gShipRecs[i].where.v < 20) {
gShipRecs[i].vel.v *= -1;
gShipRecs[i].where.v = 20;
} /* if */
if (gShipRecs[i].anim < 0)
gShipRecs[i].anim = 0;
if (++gShipRecs[i].anim > 7)
gShipRecs[i].anim = 0;
} /* if */
} /* MoveShips() */
DrawShips()
{
Rect dstRect;
extern CGrafPort *gScrapPtr, *gOSPtr;
short i, j;
extern RGBColor myWhite, myBlack;
for (i = 0; i < MAX_PLAYERS; i++)
if (gShipRecs[i].isAlive) {
dstRect.top = gOldShipRecs[i].where.v;
dstRect.bottom = dstRect.top + 15;
dstRect.left = gOldShipRecs[i].where.h;
dstRect.right = dstRect.left + 15;
RGBForeColor(&myBlack);
RGBBackColor(&myBlack);
EraseRect(&dstRect);
RGBForeColor(&myBlack);
RGBBackColor(&myWhite);
dstRect.top = gShipRecs[i].where.v;
dstRect.bottom = dstRect.top + 15;
dstRect.left = gShipRecs[i].where.h;
dstRect.right = dstRect.left + 15;
CopyBits((BitMap *) *(gScrapPtr->portPixMap),
(BitMap *) *(gOSPtr->portPixMap),
&(gShipSrcRects[i][gShipRecs[i].dir][gShipRecs[i].anim]), &dstRect, srcCopy, 0L);
for (j = 0; j < MAX_PLAYERS; j++)
if ((i != j) && CheckForShipCollision(j, dstRect)) {
KillPlayer(i);
KillPlayer(j);
} /* if */
gOldShipRecs[i] = gShipRecs[i];
} /* if */
} /* DrawShips() */
CheckForShipCollision(s, r)
short s;
Rect r;
{
Rect shipRect, tmpRect;
if (gShipRecs[s].isAlive) {
shipRect.top = gShipRecs[s].where.v;
shipRect.bottom = shipRect.top + 15;
shipRect.left = gShipRecs[s].where.h;
shipRect.right = shipRect.left + 15;
if (SectRect(&r, &shipRect, &tmpRect))
return(1);
} /* if */
return(0);
} /* CheckForShipCollision() */
KillPlayer(i)
short i;
{
extern Handle gBoomSoundH;
extern Boolean gMatchIsEnding;
extern short gMatchTicker;
extern SHIPREC gShipRecs[MAX_PLAYERS];
short j;
Rect dstRect;
gShipRecs[i].isAlive = FALSE;
dstRect.top = gShipRecs[i].where.v;
dstRect.bottom = dstRect.top + 15;
dstRect.left = gShipRecs[i].where.h;
dstRect.right = dstRect.left + 15;
RGBForeColor(&myBlack);
RGBBackColor(&myBlack);
EraseRect(&dstRect);
RGBForeColor(&myBlack);
RGBBackColor(&myWhite);
dstRect.top = gOldShipRecs[i].where.v;
dstRect.bottom = dstRect.top + 15;
dstRect.left = gOldShipRecs[i].where.h;
dstRect.right = dstRect.left + 15;
RGBForeColor(&myBlack);
RGBBackColor(&myBlack);
EraseRect(&dstRect);
RGBForeColor(&myBlack);
RGBBackColor(&myWhite);
CreateDebris(5, 25, gShipRecs[i].where.h, gShipRecs[i].where.v, gShipRecs[i].vel.h, gShipRecs[i].vel.v);
ASndPlay(gBoomSoundH);
if (!i) {
gMatchIsEnding = TRUE;
gMatchTicker = 50;
} /* if */
else {
gShipRecs[i].where.v = RngRnd(25, 455);
gShipRecs[i].where.h = RngRnd(25, 615);
if (i) {
gShipRecs[i].vel.v = RngRnd(-MAX_SPEED, MAX_SPEED);
gShipRecs[i].vel.h = RngRnd(-MAX_SPEED, MAX_SPEED);
} /* if */
else {
gShipRecs[i].vel.v = 0;
gShipRecs[i].vel.h = 0;
} /* else */
gShipRecs[i].dir = RngRnd(0, 7);
gShipRecs[i].anim = RngRnd(0, 7);
gShipRecs[i].isAlive = TRUE;
gShipRecs[i].isAccel = FALSE;
} /* else */
for (j = 1; j < MAX_PLAYERS; j++)
if (gShipRecs[j].isAlive)
return;
gMatchIsEnding = TRUE;
gMatchTicker = 50;
} /* KillPlayer() */